package yja.gzw.app.activity


import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.lifecycle.Observer
import com.kongzue.dialogx.DialogX
import com.kongzue.dialogx.dialogs.CustomDialog
import com.kongzue.dialogx.interfaces.OnBindView
import com.kongzue.dialogx.style.IOSStyle
import yja.gzw.app.R
import yja.gzw.app.base.BaseActivity
import yja.gzw.app.databinding.ActivityLogBinding
import yja.gzw.app.fragmentview.OnInputDataListener
import yja.gzw.app.fragmentview.OnInputDataListener2
import yja.gzw.app.fragmentview.YanzhengFragment
import yja.gzw.app.fragmentview.ZhanghaoFragmen
import yja.gzw.app.model.UserData
import yja.gzw.app.viewmodel.Mainviewmodel
import yja.gzw.app.window.Chicun


class LogActivity :BaseActivity<Mainviewmodel,ActivityLogBinding>(R.layout.activity_log),
    OnInputDataListener , OnInputDataListener2 {

    var sp: SharedPreferences?=null
    val chicun=Chicun()
    var isis=false
    var fralist= mutableListOf<Fragment>(
       ZhanghaoFragmen(), YanzhengFragment()
    )
    var email=""
    var password=""
    var email2=""
    var password2=""

    var zhuangtai=1



    override fun initView() {
        DialogX.init(this)
        DialogX.globalStyle = IOSStyle.style()


        binding.yonghuxieyi.setOnClickListener {
            CustomDialog.show(object : OnBindView<CustomDialog>(R.layout.layout_custom_dialog) {
                override fun onBind(dialog: CustomDialog, v: View) {
                    val tongyi = v.findViewById<TextView>(R.id.tongyi)
                    val butongyi = v.findViewById<TextView>(R.id.butongyi)
                    tongyi.setOnClickListener {
                        dialog.dismiss()
                        binding.tongyi.isChecked=true
                        isis=true
                    }
                    butongyi.setOnClickListener {

                        dialog.dismiss()
                        binding.tongyi.isChecked=false
                        isis=false
                    }

                }
            })
                .setMaskColor(resources.getColor(R.color.touming))


        }



        chicun.chicun(binding.tubiao, 110, 130)
        chicun.chicunwai(binding.tubiao, 0, 100, 0, 0)
        chicun.chicunwai(binding.danxuan,20,20,20,20)
        chicun.chicun(binding.shi1, 0,50)
        chicun.chicun(binding.shi2, 0,50)
        chicun.chicun(binding.denglu,280, 50)
        chicun.chicunwai(binding.denglu, 0, 40, 0, 0)
        chicun.chicun(binding.zhuce,280, 50)
        chicun.chicunwai(binding.zhuce, 0, 20, 0, 0)
        chicun.chicun(binding.viewpager,UserData.kuan, 200)


        sp = this.getSharedPreferences("Shujuku", Context.MODE_PRIVATE)
        binding.tongyi.setOnClickListener {
            if(isis){
                binding.tongyi.isChecked=false
                isis=!isis
            }else{
                binding.tongyi.isChecked=true
                isis=!isis

            }


        }


        binding.zhuce.setOnClickListener {
            var inten= Intent(this,ZhuceActivity::class.java)

            startActivity(inten)


        }

        binding.tiaoguo.setOnClickListener {

            var inten= Intent(this,TopshowActivity::class.java)

            startActivity(inten)

        }
        binding.viewpager.adapter=object : FragmentPagerAdapter(supportFragmentManager){
            override fun getCount(): Int {
                return fralist.size


            }

            override fun getItem(position: Int): Fragment {

                return fralist[position]

            }

        }




        binding.danxuan.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.shi1 -> {
                    binding.viewpager.currentItem = 0
                   Log.e("状态","1")
                    zhuangtai=1
                }
                R.id.shi2 -> {
                    zhuangtai=2
                    Log.e("状态","2")
                    binding.viewpager.currentItem = 1

                }
            }
        }

        binding.viewpager.addOnPageChangeListener(object : androidx.viewpager.widget.ViewPager.OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}

            override fun onPageSelected(position: Int) {
                when (position) {
                    0 -> {
                        zhuangtai=1
                        Log.e("状态","3")
                        binding.danxuan.check(R.id.shi1)
                    }


                    1 -> {
                        zhuangtai=2
                        Log.e("状态","4")
                        binding.danxuan.check(R.id.shi2)
                    }
                }
            }

            override fun onPageScrollStateChanged(state: Int) {}
        })


        binding.denglu.setOnClickListener {


            if(binding.tongyi.isChecked){
                if(zhuangtai==1){

                    viewmodel.ondengluwang(mapOf("account" to email,"password" to password))
                }else{


                    viewmodel.ondengluwang2(mapOf("mobile" to email2,"captcha" to password2))

                }
            }else{
                Toast.makeText(this, "请先勾选使用协议", Toast.LENGTH_SHORT).show()

            }






        }


    }









    override fun dataObservese() {

        viewmodel.ondenglu.observe(this, Observer{
            if(it.code==1){
                sp?.edit()?.putBoolean("islogin",true)?.apply()

                sp?.edit()?.putString("token",it.data.userinfo.token)?.apply()
                sp?.edit()?.putString("id",it.data.userinfo.id.toString())?.apply()
                UserData.token=it.data.userinfo.token
                UserData.id=it.data.userinfo.id.toString()
                Log.e("存储用户id",it.data.userinfo.id.toString())

                UserData.token=it.data.userinfo.token
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()

            val inten= Intent(this,TopshowActivity::class.java)

            startActivity(inten)
                finish()
            }else{
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()

            }

        })
        viewmodel.ondenglu2.observe(this, Observer{
            if(it.code==1){
                UserData.token=it.data.userinfo.token
                sp?.edit()?.putBoolean("islogin",true)?.apply()

                sp?.edit()?.putString("token",it.data.userinfo.token)?.apply()
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()

                val inten= Intent(this,TopshowActivity::class.java)

                startActivity(inten)
                finish()

            }else{
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()

            }

        })



    }

    override fun onInputData(email: String, password: String) {
       this. email=email
        this. password=password

    }

    override fun onInputData2(email: String, password: String) {
        this. email2=email
        this. password2=password
    }
}